package com.eva.admin.biz.cms.resource;

import com.eva.admin.biz.cms.resource.dto.QueryCmsResourceGroupDTO;
import com.eva.admin.biz.cms.resource.dto.UpdateCmsResourceGroupDTO;
import com.eva.admin.biz.cms.resource.vo.CmsResourceGroupVO;
import com.eva.admin.biz.cms.resource.dto.CreateCmsResourceGroupDTO;
import com.eva.dao.cms.model.CmsResource;
import com.eva.framework.common.constants.ResponseStatus;
import com.eva.framework.common.exception.BusinessException;
import com.eva.admin.dao.cms.CmsResourceGroupMapper;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import com.eva.framework.common.utils.AssertUtil;
import com.eva.dao.cms.model.CmsResourceGroup;
import com.eva.framework.mp.utils.PageUtil;
import com.eva.service.cms.CmsResourceGroupService;
import com.eva.service.cms.CmsResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CmsResourceGroupBiz {

    @Resource
    private CmsResourceGroupMapper cmsResourceGroupMapper;

    @Resource
    private CmsResourceGroupService cmsResourceGroupService;

    @Resource
    private CmsResourceService cmsResourceService;

    /**
     * 创建
     *
     * @param dto 创建参数
     */
    @Transactional
    public void create(CreateCmsResourceGroupDTO dto) {
        // 验证
        AssertUtil.notEmpty(dto.getUid(), "唯一标识不能为空");
        AssertUtil.notEmpty(dto.getName(), "组名不能为空");
        // 验证唯一标识
        if (cmsResourceGroupService.exists(CmsResourceGroup.builder().uid(dto.getUid()).build())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST, "唯一标识已存在");
        }
        // 构建对象
        CmsResourceGroup newRecord = new CmsResourceGroup();
        BeanUtils.copyProperties(dto, newRecord);
        // 执行创建
        cmsResourceGroupService.create(newRecord);
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     */
    @Transactional
    public void deleteById(Integer id) {
        AssertUtil.notEmpty(id, "主键不能为空");
        // 删除组
        cmsResourceGroupService.deleteById(id);
        // 删除组内资源
        cmsResourceService.delete(CmsResource.builder().groupId(id).build());
    }

    /**
     * 根据主键批量删除
     *
     * @param ids 主键集合
     */
    @Transactional
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    /**
     * 修改
     *
     * @param dto 修改参数
     */
    public void updateById(UpdateCmsResourceGroupDTO dto) {
        // 验证
        AssertUtil.notEmpty(dto.getUid(), "唯一标识不能为空");
        AssertUtil.notEmpty(dto.getName(), "组名不能为空");
        // 验证唯一标识
        if (cmsResourceGroupService.exists(CmsResourceGroup.builder().id(dto.getId()).uid(dto.getUid()).build())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST, "唯一标识已存在");
        }
        // 构建对象
        CmsResourceGroup newRecord = CmsResourceGroup.builder().build();
        BeanUtils.copyProperties(dto, newRecord);
        // 执行修改
        cmsResourceGroupService.updateById(newRecord);
    }

    /**
     * 分页查询
     *
     * @param pageWrap 分页参数
     * @return 分页数据
     */
    public PageData<CmsResourceGroupVO> findPage(PageWrap<QueryCmsResourceGroupDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<CmsResourceGroupVO> result = cmsResourceGroupMapper.search(pageWrap.getModel());
        return PageUtil.from(new PageInfo<>(result));
    }
}
